package com.xinyue.sparkle.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xinyue.sparkle.bean.User;
import com.xinyue.sparkle.dao.UserDao;


@Repository("UserDaoImpl")
public class UserDaoImpl implements UserDao{

	@Autowired
    private SessionFactory factory;
	
	/**hibernate对JDBC的支持
	public void jdbc() {
		factory.getCurrentSession().doWork(new Work() {
			public void execute(Connection conn) throws SQLException {
				PreparedStatement ps = conn.prepareStatement("");
				ps.setString(0, "");
				ResultSet rs = ps.executeQuery();
				String name = rs.getString("name");
			}
		});
	}*/


	public void save(User user) {
		factory.getCurrentSession().save(user);
	}
	public void saveOrUpdate(User user) {
		factory.getCurrentSession().saveOrUpdate(user);
	}

	public void update(User user) {
		factory.getCurrentSession().update(user);
	}

	public void delete(User user) {
		factory.getCurrentSession().delete(user);
	}
	public void delete(List<User> users) {
		if(users != null && users.size() > 0) {
			for (int i = 0; i < users.size(); i++) {
				delete(users.get(i));
			}
		}
	}
	public void deleteById(int id) {
		factory.getCurrentSession();
	}

	@SuppressWarnings("unchecked")
	public List<User> queryAll() {
		return factory.getCurrentSession().createQuery("from User").list();
	}
	@SuppressWarnings("unchecked")
	public List<User> queryUser(User user) {
		return factory.getCurrentSession().createQuery("from User").list();
	}
	public User queryUserbyId(int id) {
		return null;
	}
}
